package net.xo68.destiny.core.eventbus;

import io.jsonwebtoken.RequiredTypeException;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;

/**
 * 消息事件
 * @author wuxie
 * @version 2020-9-24
 */
@ToString
@Data
public class MessageEvent implements Serializable {

    private int eventType;

    private Object body;

    public <T> T mesageBody(Class<T> requiredType){
        if(body==null){
            return null;
        }
        if(!requiredType.isInstance(this.body)){
            throw new RequiredTypeException("Expected value to be of type: " + requiredType + ", but was " + this.body.getClass());
        }
        return requiredType.cast(this.body);
    }
}
